package com.kk.base.controller.home;

import com.kk.base.bean.CodeMsg;
import com.kk.base.bean.PageBean;
import com.kk.base.bean.Result;
import com.kk.base.entity.home.HomeUser;
import com.kk.base.entity.home.HomeUserAddress;
import com.kk.base.entity.home.HomeUserTypeEnum;
import com.kk.base.service.home.HomeUserAddressService;
import com.kk.base.util.SessionUtil;
import com.kk.base.util.ValidateEntityUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @info :  前台用户地址控制层1
 */
@RequestMapping("/home/address")
@Controller
public class HomeUserAddressController {

    @Autowired
    private HomeUserAddressService homeUserAddressService;


    /**
     * 跳转到地址中心页面
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String address(Model model, PageBean<HomeUserAddress> pageBean,String search,String searchCategory) {
        HomeUser homeUser = SessionUtil.getHomeUser();
        pageBean.setPageSize(5);
        model.addAttribute("homeUser", homeUser);
        model.addAttribute("search", search);
        model.addAttribute("searchCategory", searchCategory);
        model.addAttribute("pageBean", homeUserAddressService.findList(pageBean,search,searchCategory));
        model.addAttribute("SENDER", HomeUserTypeEnum.SENDER);
        model.addAttribute("RECIPIENT", HomeUserTypeEnum.RECIPIENT);
        model.addAttribute("index", 4);
        model.addAttribute("active", 1);
        return "home/user_manager/address";
    }


    /**
     * 新增用户地址
     * @param homeUserAddress
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public Result<Boolean> add(HomeUserAddress homeUserAddress) {
        HomeUser homeUser = SessionUtil.getHomeUser();
        CodeMsg validate = ValidateEntityUtil.validate(homeUserAddress);
        if (validate.getCode() != CodeMsg.SUCCESS.getCode()) {
            return Result.error(validate);
        }
        if (homeUserAddress.getMobile() == null && homeUserAddress.getPhone() == null) {
            return Result.error(CodeMsg.HOMEUSERADDRESS_MOBILE_PHONE_ERROR);
        }
        if ("".equals(homeUserAddress.getMobile()) && "".equals(homeUserAddress.getPhone())) {
            return Result.error(CodeMsg.HOMEUSERADDRESS_MOBILE_PHONE_ERROR);
        }
        homeUserAddress.setHomeUser(homeUser);
        if (homeUserAddress.getIsDefault() == 0) {
            HomeUserAddress byIsDefault = homeUserAddressService.findByIsDefaultAndHomeUserTypeAndHomeUser_Id(
                    HomeUserAddress.DEFAULT,homeUserAddress.getHomeUserType(),homeUser.getId());
            if (byIsDefault != null) {
                byIsDefault.setIsDefault(1);
                if (homeUserAddressService.save(byIsDefault) == null) {
                    return Result.error(CodeMsg.HOMEUSERADDRESS_ADD_ERROR);
                }
            }
        }
        if (homeUserAddressService.save(homeUserAddress) == null) {
            return Result.error(CodeMsg.HOMEUSERADDRESS_ADD_ERROR);
        }
        return Result.success(true);
    }


    /**
     * 点击修改时弹出层要回显数据
     *
     * @param id
     * @return
     */
    @RequestMapping("/initializeForm")
    @ResponseBody
    public Result<HomeUserAddress> initializeForm(@RequestParam("id") Long id) {
        HomeUserAddress homeUserAddress = homeUserAddressService.find(id);
        return Result.success(homeUserAddress);
    }

    /**
     * 用户修改地址
     * @param homeUserAddress
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public Result<Boolean> update(HomeUserAddress homeUserAddress) {
        HomeUser homeUser = SessionUtil.getHomeUser();
        CodeMsg validate = ValidateEntityUtil.validate(homeUserAddress);
        if (validate.getCode() != CodeMsg.SUCCESS.getCode()) {
            return Result.error(validate);
        }
        if (homeUserAddress.getMobile() == null && homeUserAddress.getPhone() == null) {
            return Result.error(CodeMsg.HOMEUSERADDRESS_MOBILE_PHONE_ERROR);
        }
        if ("".equals(homeUserAddress.getMobile()) && "".equals(homeUserAddress.getPhone())) {
            return Result.error(CodeMsg.HOMEUSERADDRESS_MOBILE_PHONE_ERROR);
        }
        if (homeUserAddress.getIsDefault() == 0) {
            HomeUserAddress byIsDefault = homeUserAddressService.findByIsDefaultAndHomeUserTypeAndHomeUser_Id(
                    HomeUserAddress.DEFAULT,homeUserAddress.getHomeUserType(),homeUser.getId());
            if (byIsDefault != null) {
                byIsDefault.setIsDefault(1);
                if (homeUserAddressService.save(byIsDefault) == null) {
                    return Result.error(CodeMsg.HOMEUSERADDRESS_ADD_ERROR);
                }
            }
        }
        HomeUserAddress byId = homeUserAddressService.find(homeUserAddress.getId());
        //讲提交的管理员信息指定字段复制到已存在的user对象中,该方法会覆盖新字段内容
        BeanUtils.copyProperties(homeUserAddress, byId, "id", "createTime", "updateTime", "homeUser");
        if (homeUserAddressService.save(byId) == null) {
            return Result.error(CodeMsg.HOMEUSERADDRESS_ADD_ERROR);
        }
        return Result.success(true);
    }


    /**
     * 删除地址
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Result<Boolean> delete(@RequestParam(name = "id") Long id) {
        try {
            homeUserAddressService.delete(id);
        } catch (Exception e) {
            return Result.error(CodeMsg.HOMEUSERADDRESS_DELETE_ERROR);
        }
        return Result.success(true);
    }


    /**
     * 修改默认地址
     * @param id
     * @return
     */
    @RequestMapping("/defaultAddress")
    @ResponseBody
    public Result<Boolean> defaultAddress(@RequestParam(name = "id") Long id) {
        HomeUser homeUser = SessionUtil.getHomeUser();
        HomeUserAddress homeUserAddress = homeUserAddressService.find(id);
        HomeUserAddress byIsDefault = homeUserAddressService.findByIsDefaultAndHomeUserTypeAndHomeUser_Id(
                HomeUserAddress.DEFAULT,homeUserAddress.getHomeUserType(),homeUser.getId());
        if (byIsDefault != null) {
            byIsDefault.setIsDefault(1);
            if (homeUserAddressService.save(byIsDefault) == null) {
                return Result.error(CodeMsg.HOMEUSERADDRESS_DEFAULT_ERROR);
            }
        }
        homeUserAddress.setIsDefault(0);
        if (homeUserAddressService.save(homeUserAddress) == null) {
            return Result.error(CodeMsg.HOMEUSERADDRESS_DEFAULT_ERROR);
        }
        return Result.success(true);
    }


}

